package cn.gailvlun.gll.util

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import cn.gailvlun.gll.R
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.emotion.ReportReq
import cn.gailvlun.gll.net.emotion.ReportRes


class ReportUtil(val mActivity: Activity) {
    private var mCausePopup: PopupWindow
    private var layoutInflater: LayoutInflater
    private var parent: ViewGroup
    private val req = ReportReq()


    fun show() {
        mCausePopup.showAtLocation(parent, Gravity.BOTTOM, 0, 0)
    }

    init {
        layoutInflater = LayoutInflater.from(mActivity)
        val causeView = layoutInflater.inflate(R.layout.popup_report_cause1, null, false)
        parent = mActivity.findViewById(android.R.id.content)
        mCausePopup = PopupWindow(causeView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        mCausePopup.setBackgroundDrawable(BitmapDrawable())

        req.targetType = "EmotionRecord"
        req.reasonType = "others"
        req.targetId = 0
        causeView.findViewById<TextView>(R.id.tv_cause0).setOnClickListener { _ ->
            mCausePopup.dismiss()
            report(req)

        }
        causeView.findViewById<TextView>(R.id.tv_cause1).setOnClickListener { _ ->
            mCausePopup.dismiss()
            report(req)

        }
        causeView.findViewById<TextView>(R.id.tv_cause2).setOnClickListener { _ ->
            mCausePopup.dismiss()
            report(req)

        }
        causeView.findViewById<TextView>(R.id.tv_cancel).setOnClickListener { _ ->
            mCausePopup.dismiss()
        }
    }


    private fun report(req: ReportReq?) {
        val pd = DialogUtil.showIndeterminate(mActivity)
        HttpMethods.getEmotionService()
                .report(req)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<ReportRes>() {
                    override fun onSuccess(response: ReportRes?) {
                        pd.dismiss()
                        mCausePopup.dismiss()
                        ToastUtil.showShort("举报成功")
                    }

                    override fun onFailure(errMsg: String?, response: ReportRes?, code: Int) {
                        pd.dismiss()
                        ToastUtil.showShort("举报成功")

                    }
                })
    }


}